package ngocthien.game;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.graphics.Color;

public class StateMenu<sMenu> extends BaseGameActivity implements
		IOnMenuItemClickListener {
	
	private static final int MENU_START=0;
	private static final int MENU_OPTION=1;
	private static final int MENU_ABOUT=2;
	private static final int MENU_QUIT=3;

	private MenuScene mMenuScene;
	
	public Engine onLoadEngine() {
		// TODO Auto-generated method stub
		Global.mCamera = new Camera(0, 0, Global.CAMERA_WIDTH,
				Global.CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(Global.CAMERA_WIDTH,
						Global.CAMERA_HEIGHT), Global.mCamera));
	}

	public void onLoadResources() {
		// TODO Auto-generated method stub
		
		Global.mBackgroundTexture=new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		FontFactory.setAssetBasePath("font/");
		Global.mFont=FontFactory.createFromAsset(Global.mBackgroundTexture, this, "Daville Slanted.ttf", 50, true, Color.WHITE);
		this.mEngine.getTextureManager().loadTexture(Global.mBackgroundTexture);
		this.mEngine.getFontManager().loadFont(Global.mFont);
	}

	public Scene onLoadScene() {
		// TODO Auto-generated method stub

		this.mEngine.registerUpdateHandler(new FPSLogger());

		this.mMenuScene=this.createMenuScene();
		
		Global.mMainScene = new Scene();
		Global.mMainScene.setBackground(new ColorBackground(1f, 1f, 1f));
		
		
		Global.mMainScene.setChildScene(this.mMenuScene, false, true, true);
		
		return Global.mMainScene;
	}

	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		// TODO Auto-generated method stub
		
		switch (pMenuItem.getID()) {
		case MENU_START:
			Intent intent=new Intent(StateMenu.this,StateGame.class);
			startActivity(intent);
			return true;
		case MENU_OPTION:
			Intent intent1=new Intent(StateMenu.this,StateOption.class);
			startActivity(intent1);
			return true;
		case MENU_ABOUT:
			Intent intent2=new Intent(StateMenu.this,StateAbout.class);
			startActivity(intent2);
			return true;
		case MENU_QUIT:
			this.finish();
			return true;
		default:
			return false;
		}
	}
	
	private MenuScene createMenuScene(){
		final MenuScene menuScene=new MenuScene(Global.mCamera);
		
		menuScene.setPosition(10, 10);
		
		final IMenuItem startGame=new ColorMenuItemDecorator(new TextMenuItem(MENU_START, Global.mFont, "Start"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
		startGame.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(startGame);

		final IMenuItem optionGame=new ColorMenuItemDecorator(new TextMenuItem(MENU_OPTION, Global.mFont, "Option"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
		startGame.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(optionGame);
		
		final IMenuItem aboutGame=new ColorMenuItemDecorator(new TextMenuItem(MENU_ABOUT, Global.mFont, "About"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
		startGame.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(aboutGame);
		
		final IMenuItem quitGame=new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, Global.mFont, "Quit"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
		startGame.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(quitGame);
		
		menuScene.buildAnimations();
		menuScene.setBackgroundEnabled(false);
		menuScene.setOnMenuItemClickListener(this);
		return menuScene;
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		StateMenu.this.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

}
